package com.xishu.controller;

import com.xishu.entity.activity.Activity;
import com.xishu.entity.shop.Shop;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ShopService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
public class ActivityController {

    private CommonService commonService = CommonService.getInstance();
    private ShopService shopService = ShopService.getInstance();

    @ApiModelProperty(value = "添加活动")
    @PostMapping(value = ("/activity/register"), produces = ("application/json;charset=UTF-8"))
    public ResponseData register(@RequestBody Activity activity) throws Exception {
        VerifyUtil.isEmpty(activity.getShopId(), activity.getTittle());
        return commonService.createObject(activity, null, true, ResponseStatus.NAME_EXISTS);
    }

    @ApiOperation(value = "删除活动")
    @DeleteMapping(value = ("/activity/delete"), produces = ("application/json;charset=UTF-8"))
    public ResponseData delete(@RequestBody Activity activity) throws Exception {
        return commonService.delete(activity);
    }

    @ApiOperation(value = "修改活动")
    @PutMapping(value = ("/activity/modify"), produces = ("application/json;charset=UTF-8"))
    public ResponseData modify(@RequestBody Activity activity) throws Exception {
        return CommonService.getInstance().updateObject(activity);
    }

    @ApiOperation(value = "查询活动")
    @PutMapping(value = ("/activity/search"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody Activity activity) throws Exception {
        VerifyUtil.verify(()->activity.getShopId()!=null);
        return commonService.searchResponse(activity, (a) -> {
            Long shopId = a.getShopId();
            try {
                Shop shop = shopService.findShopById(shopId);
                a.setShopName(shop.getName());
                a.setShopName_zh(shop.getName_zh());
                a.setShopName_en(shop.getName_en());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
}
